package com.mano233.server.config.security;

import com.mano233.server.mapper.UserMapperCustom;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Objects;

/**
 * UserDetailsService实现类
 * @author mano233
 */
@Service
public class UserCustomDetailServiceImpl implements UserDetailsService, Serializable {
    private final UserMapperCustom userMapper;
    private static Logger logger = LoggerFactory.getLogger(UserCustomDetailServiceImpl.class);

    public UserCustomDetailServiceImpl(UserMapperCustom userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public UserCustomDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        UserCustomDetails userInfo = userMapper.getUserByName(userName);
        if (Objects.isNull(userInfo)) {
            throw new UsernameNotFoundException(userName);
        }
        return userInfo;
    }

    public UserCustomDetails loadUserById(int uid) throws UsernameNotFoundException {
        UserCustomDetails userInfo = userMapper.getUserById(uid);
        if (Objects.isNull(userInfo)) {
            throw new UsernameNotFoundException(uid + "");
        }
        return userInfo;
    }
}
